package cn.lzk._02_xhr;


import org.apache.rocketmq.client.exception.MQClientException;
import org.apache.rocketmq.client.producer.DefaultMQProducer;
import org.apache.rocketmq.client.producer.SendCallback;
import org.apache.rocketmq.client.producer.SendResult;
import org.apache.rocketmq.common.message.Message;

import java.nio.charset.StandardCharsets;

public class Producer {
    public static void main(String[] args) throws Exception {
        //创建生产者
        DefaultMQProducer producer = new DefaultMQProducer("xhr_producer");
        //设置nameserver地址
        producer.setNamesrvAddr("localhost:9876");
        //启动生产者
        producer.start();
        //创建消息体
        Message message = new Message("xhr", "hello", "这是我第二遍作业".getBytes(StandardCharsets.UTF_8));
        //发送异步消息
        producer.send(message, new SendCallback() {
            @Override
            public void onSuccess(SendResult sendResult) {
                //发送成功
                System.out.println(sendResult);
            }

            @Override
            public void onException(Throwable throwable) {
                //发送失败打印异常
                System.out.println(throwable.getMessage());
            }
        });
        //关闭生产者
        producer.shutdown();

    }
}
